﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;

namespace ToolExcel
{
    public static partial class XTool
    {
        public static StringBuilder sbJsonJavaScript = new StringBuilder();

        public static void 导出ClassClientJavaScript()
        {
            if (!全部导出)
            {
                return;
            }
            导出ClassClientJavaScriptEnum();
            导出ClassClientJavaScriptJson();
        }
        public static void 导出ClassClientJavaScriptEnum()
        {
            Dictionary<string, Dictionary<string, string>> DictDictEnums = new Dictionary<string, Dictionary<string, string>>();
            foreach (var kvp in DictDictEnum1) { DictDictEnums[kvp.Key] = kvp.Value; }
            foreach (var kvp in DictDictEnum2) { DictDictEnums[kvp.Key] = kvp.Value; }

            var sbw = new StringBuilder();
            sbw.AppendLine("/* eslint-disable comma-dangle */");
            sbw.AppendLine("/* eslint-disable no-unused-vars */");

            var sbdefault = new StringBuilder();
            sbdefault.AppendLine("export default {");

            foreach (var kvp in DictDictEnums)
            {
                sbw.Append($"var confem{kvp.Key} = {{\r\n");
                long min = 0, max = 0;
                foreach (var kvp2 in kvp.Value)
                {
                    sbw.Append($"  {kvp2.Key}: {kvp2.Value},\r\n");
                    long.TryParse(kvp2.Value, out long tempmax);
                    min = Math.Min(min, tempmax);
                    max = Math.Max(max, tempmax);
                }
                sbw.Append($"}}\r\n");
                sbdefault.AppendLine($"  confem{kvp.Key}: confem{kvp.Key},");
            }
            foreach (var kvp in DictDictEnums)
            {
                sbw.Append($"var confemm{kvp.Key} = {{\r\n");
                long min = 0, max = 0;
                foreach (var kvp2 in kvp.Value)
                {
                    sbw.Append($"  {kvp2.Value}: \'{kvp2.Key}\',\r\n");
                    long.TryParse(kvp2.Value, out long tempmax);
                    min = Math.Min(min, tempmax);
                    max = Math.Max(max, tempmax);
                }
                sbw.Append($"}}\r\n");
                sbdefault.AppendLine($"  confemm{kvp.Key}: confemm{kvp.Key},");
            }

            sbdefault.AppendLine("}");
            sbw.Append(sbdefault);


            string path_class = PathClass + "\\" + "confenum.js";
            XGlobal.DeleteFile(path_class);
            FileStream fs_class = new FileStream(path_class, FileMode.OpenOrCreate);
            StreamWriter sw_class = new StreamWriter(fs_class);
            sw_class.Write(sbw);
            sw_class.Close();
        }
        public static void 导出ClassClientJavaScriptJson()
        {
            var sbw = new StringBuilder();

            sbw.AppendLine("/* eslint-disable comma-dangle */");
            sbw.AppendLine("/* eslint-disable no-unused-vars */");

            var sbdefault = new StringBuilder();
            sbdefault.AppendLine("export default {");

            foreach (var nn in DictPages.Values)
            {
                if (!(nn.ValidType == EValidType.公共 || nn.ValidType == EValidType.仅客户端))
                {
                    continue;
                }
                sbw.AppendLine($"import {nn.Name} from '../confjson/{nn.Name}.json'");
                sbdefault.AppendLine($"  {nn.Name}: {nn.Name},");
            }

            sbdefault.AppendLine("}");
            sbw.Append(sbdefault);

            string path_class = PathClass + "\\" + "confjson.js";
            XGlobal.DeleteFile(path_class);
            FileStream fs_class = new FileStream(path_class, FileMode.OpenOrCreate);
            StreamWriter sw_class = new StreamWriter(fs_class);
            sw_class.Write(sbw);
            sw_class.Close();
        }
    }
}
